package org.eclipse.emf.index.omds.ui.content;

import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jst.j2ee.common.internal.impl.J2EEEObjectImpl;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPluginResourceHandler;
import org.eclipse.jst.j2ee.internal.provider.J2EEItemProvider;
import org.eclipse.jst.javaee.ejb.SessionBean;
import org.eclipse.jst.javaee.ejb.internal.impl.SessionBeanImpl;
import org.eclipse.jst.jee.ui.plugin.JEEUIPluginIcons;
import org.eclipse.swt.graphics.Image;

public class OMSLableProvider extends LabelProvider{
	
	private static Image SERVLET = null;
	private static Image SESSIONBEAN = null;
	private static Image MESSAGEDRIVENBEAN = null;
	private static Image LISTENER = null;
	private static Image FILTER = null;	
	
	public Image getImage(Object element) {
		if(element instanceof OMSResourceObject){
			OMSResourceObject temp = (OMSResourceObject)element;
			
			//Checks the type of the resource and assign the respective icon.
			if("Servlet".equalsIgnoreCase(temp.getResourceType())){
				return getServletImage();
			}
			
			else if("Listener".equalsIgnoreCase(temp.getResourceType())){
				return getListenerImage();
			}
			
			else if("Filter".equalsIgnoreCase(temp.getResourceType())){
				return getFilterImage();
			}
			
			else if("SessionBean".equalsIgnoreCase(temp.getResourceType())){
				return getSessionBeanImage();
			}
			
			else if("MessageDrivenBean".equalsIgnoreCase(temp.getResourceType())){
				return getMsgDrivenBeanImage();
			}
			
		}
		return null;
	}
	
	private Image getMsgDrivenBeanImage() {
		if (MESSAGEDRIVENBEAN == null) {
	        URL url = (URL) J2EEPlugin.getPlugin().getImage("message_bean_obj");
	        ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
	        MESSAGEDRIVENBEAN = imageDescriptor.createImage();
	    }
	    return MESSAGEDRIVENBEAN;
	}

	private Image getSessionBeanImage() {
		if (SESSIONBEAN == null) {
	        URL url = (URL) J2EEPlugin.getPlugin().getImage("sessionBean_obj");
	        ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
	        SESSIONBEAN = imageDescriptor.createImage();
	    }
	    return SESSIONBEAN;
	}

	private Image getFilterImage() {
		if (FILTER == null) {
	        URL url = (URL) J2EEPlugin.getPlugin().getImage(JEEUIPluginIcons.GROUP_FILTER_MAPPING);
	        ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
	        FILTER = imageDescriptor.createImage();
	    }
	    return FILTER;
	}

	private Image getListenerImage() {
		if (LISTENER == null) {
	        URL url = (URL) J2EEPlugin.getPlugin().getImage(JEEUIPluginIcons.GROUP_LISTENER);
	        ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
	        LISTENER = imageDescriptor.createImage();
	    }
	    return LISTENER;
	}

	private Image getServletImage() {
		if (SERVLET == null) {
	        URL url = (URL) J2EEPlugin.getPlugin().getImage(JEEUIPluginIcons.GROUP_SERVLET);
	        ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
	        SERVLET = imageDescriptor.createImage();
	    }
	    return SERVLET;
	}

	public String getText(Object element){
		if(element instanceof OMSResourceObject){
			OMSResourceObject temp = (OMSResourceObject)element;
			return temp.getResourceName();
		}
		return null;		
	}

}
